<!--
  功能：报名列表
  作者：licy
  邮箱：licy@briup.com
  时间：2020年11月30日 14:10:00
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class='student_list'>
    <!-- 按钮 -->
    <div class="btns">
      <div style="text-align:center;margin-bottom:.5em">
        <el-form size="small" :inline='true'>
          <el-form-item>
            <el-date-picker style="width:120px" v-model="param.year" value-format="yyyy" type="year" placeholder="毕业年份"> </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-cascader filterable @change="cascaderChangeHandler" v-model="param.channelIds" clearable :options="channels" placeholder="请选择渠道" :props="{ checkStrictly: true ,children:'children',label:'name',value:'id', emitPath: false,multiple:true}" ></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-cascader filterable v-model="param.clazzId" clearable :options="directionClazzTree" placeholder="请选择班级"
          :props="{ children:'clazzes',label:'name',value:'id',emitPath: false}" ></el-cascader>
          </el-form-item>
          <el-form-item >
            <el-select v-model="param.status" placeholder="状态" style="width:120px" clearable>
              <el-option value="报名中" label="报名中"></el-option>
              <el-option value="待选方向" label="待选方向"></el-option>
              <el-option value="待分班" label="待分班"></el-option>
              <el-option value="学习中" label="学习中"></el-option>
              <el-option value="离校" label="离校"></el-option>
            </el-select>  
          </el-form-item>
          <el-form-item >
            <el-input placeholder="姓名" v-model="param.realname" ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div style="text-align:right">
        <el-button size="small" icon="el-icon-search" type="primary" @click="loadStudents">搜索</el-button>
        <el-button size="small" type="primary" @click="toBatchEditTuitionHandler">批量设置学费</el-button>
        <el-button type="primary" icon="el-icon-download" size="small" @click="downloadHandler">导出</el-button>
      </div>
    </div>
    <!-- 按钮 -->
    <!-- 表格 -->
    <el-table :data="studentData.list" size="small" v-loading="loading" 
    :row-class-name="attentionStudentClass"
    @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed="left" > </el-table-column>
      <el-table-column type="index" :index="1" label="序号" align="center" fixed="left"/>
      <el-table-column prop="realname" label="姓名" width="90" align="center" fixed="left"/>
      <el-table-column prop="status" label="状态" width="80" align="center" fixed="left">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status ==='报名中'" type="danger" size="mini">{{scope.row.status}}</el-tag>
          <el-tag v-else-if="scope.row.status==='待分班'" type="warning" size="mini">{{scope.row.status}}</el-tag>
          <el-tag v-else type="success" size="mini">{{scope.row.status}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="渠道" width="300" align="center">
        <template slot-scope="scope">
            <div v-if="scope.row.channel">
              {{scope.row.channel.parent.parent?scope.row.channel.parent.parent.name+" - ":""}}
              {{scope.row.channel.parent?scope.row.channel.parent.name+" - ":""}}
              {{scope.row.channel.name}}
            </div>
            <div v-else>
              暂无渠道信息
            </div>
          </template>
      </el-table-column>
      <el-table-column prop="schoolStuClazz" label="校内班级" width="150" align="center"/>
      <el-table-column prop="schoolStuNo" label="校内学号" width="150" align="center"/>
      <el-table-column prop="ddTel" label="钉钉手机号" width="120" align="center"/>
      <el-table-column prop="stuType" label="报名方式" width="80" align="center"/>
      <el-table-column prop="applyTime" label="报名时间" width="150" align="center">
        <template slot-scope="scope">
          {{scope.row.applyTime | fmtTime}}
        </template>
      </el-table-column>
      <el-table-column prop="idCard" label="身份证号" width="180" align="center"/>
      <el-table-column prop="gender" label="性别" width="60" align="center"/>
      <el-table-column prop="birth" label="出生日期" width="120" align="center">
        <template slot-scope="scope">
          {{scope.row.birth | fmtDate}}
        </template>
      </el-table-column>
      <el-table-column prop="education" label="学历" width="80" align="center"/>
      <el-table-column prop="major" label="专业" width="150" align="center"/>
      <el-table-column prop="graduationTime" label="毕业时间" width="120" align="center">
        <template slot-scope="scope">
          {{scope.row.graduationTime | fmtDate}}
        </template>
      </el-table-column>
      <el-table-column prop="flp" label="外语水平" width="80" align="center"/>
      <el-table-column prop="tel" label="手机号" width="120" align="center"/>
      <el-table-column prop="guardianTel" label="监护人手机号" width="120" align="center"/>
      <el-table-column prop="qq" label="qq号" width="120" align="center"/>
      <el-table-column prop="homeAddress" label="家庭住址" width="350" align="center"/>
      <el-table-column prop="isAttention" label="关注" width="120" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isAttention == '关系学生'" type="primary" size="small">{{scope.row.isAttention}}</el-tag>
          <el-tag v-else-if="scope.row.isAttention == '问题学生'" type="warning" size="small">{{scope.row.isAttention}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="comment" label="备注" width="180" align="center"/>
      <el-table-column label="籍贯" prop="nativePlace" width="200"></el-table-column>
      <el-table-column label="政治面貌" prop="politicsStatus" width="100"></el-table-column>
      <el-table-column prop="tuition" label="学费" width="80" align="center"/>
      <el-table-column prop="reserve" label="订座费" width="80" align="center"/>
      <el-table-column label="操作" align="center" width="160" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" size="mini" v-if="scope.row.isAttention" @click.prevent="toCancelAttentionHandler(scope.row)">取消关注</el-button>
          <el-button type="text" size="mini" v-else @click.prevent="toAttentionHandler(scope.row)">关注</el-button>
          <el-button type="text" size="mini" :disabled="scope.row.location === '基地'"  @click.prevent="toReportHandler(scope.row)">报道</el-button>
          <el-button type="text" size="mini" @click.prevent="toEditTuitionHandler(scope.row)">学费</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- /表格 -->
    <!-- 分页 -->
    <div class="page">
        <el-pagination background 
          layout="prev, pager, next,sizes" 
          small
          :total="studentData.total" 
          :page-size="studentData.pageSize" 
          :current-page="studentData.page"
          :page-sizes="[10, 15, 20,25,50,100]"
          @size-change="handleSizeChange"
          @current-change="currentChangeHandler">
        </el-pagination>
    </div>
    <!-- /分页 -->
     <!-- 抽屉 -->
    <Briupdrawer :drawer-visible="visible" title="重点关注" width="40%" @on-change-visible="handlerVisibleChange" >
      <div slot="content">
        <el-form :model="form" ref="attentionForm" :rules="rules_attention" size="small">
          <el-form-item label="关注类型" prop="type">
           <div style="clear:both;text-align:center">
              <el-radio-group v-model="form.type">
                <el-radio label="关系学生" value="关系学生"></el-radio>
                <el-radio label="问题学生" value="问题学生"></el-radio>
              </el-radio-group>
           </div>
          </el-form-item>
          <el-form-item label="备注" prop="comment">
            <el-input type="textarea" v-model="form.comment" :rows="5"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取消</el-button>
        <el-button size="small" type="primary" @click="submitAttentionHandler">确定</el-button>
      </span>
    </Briupdrawer>
    <!-- 抽屉结束 -->
    <!-- 报道 -->
    <el-dialog title="确认报道信息" width="50%" :visible.sync="visible_report" class="customer_modal">
      <p class="red_info"> <strong>注意！</strong> 报道当天确认宿舍编号，住宿费从报道日开始算起</p>
      <el-form label-width="80px" :model="form_report" ref="report_form" :rules="rules_report" >
        <el-form-item label="宿舍类型" prop="dormitoryType">
          <el-select v-model="form_report.dormitoryType">
            <el-option v-for="d in dormitoryTypes" :key="d.id" :label="d.name" :value="d.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍编号" prop="dormitory">
          <el-input v-model.number="form_report.dormitory"></el-input>
        </el-form-item>
        <el-form-item label="报道日期">
          <el-date-picker v-model="form_report.reportTime" value-format="timestamp" type="date" placeholder="选择日期" />
        </el-form-item>
      </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible_report = false">取消</el-button>
        <el-button size="small" type="primary" @click="reportHandler">确认</el-button>
      </span>
    </el-dialog>
    <!-- 报道 -->

    <el-dialog title="学费信息" width="50%" :visible.sync="visible_tuition" class="customer_modal">
      <el-form label-width="80px" :model="form_tuition" ref="confirm_apply_form" :rules="rules_tuition" >
        <el-form-item label="学费" prop="tuition">
          <el-input v-model.number="form_tuition.tuition"></el-input>
        </el-form-item>
        <el-form-item label="是否订座">
          <el-radio-group v-model="form_tuition.isReserve">
            <el-radio :label="0">无</el-radio>
            <el-radio :label="1">有</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="订座费" v-if="form_tuition.isReserve === 1">
          <el-input v-model.number="form_tuition.reserve"></el-input>
        </el-form-item>
      </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible_tuition = false">取消</el-button>
        <el-button size="small" type="primary" @click="editTuitionHandler">确认</el-button>
      </span>
    </el-dialog>
    <!-- /确认报名 -->

  </div>
</template>

<script>
import {get,post} from '@/utils/request'
import _ from 'lodash'
import { mapGetters } from 'vuex'
import qs from 'qs'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
      form: {},
      loading:false,
      visible:false,
      visible_tuition:false,
      form_tuition:{
      },
      param:{
        page:1,
        pageSize:15
      },
      students:[],// 批量操作的值
      dormitoryTypes:[],
      studentData:{list:[]},
      rules_tuition:{
        tuition: [ { required: true, message: '请输入学费信息', trigger: 'blur' } ]
      },
      rules_report:{
        dormitoryType: [ { required: true, message: '请选择宿舍类型', trigger: 'change' } ],
        dormitory: [ { required: true, message: '请输入宿舍编号', trigger: 'blur' } ],
      },
      rules_attention:{
        type: [ { required: true, message: '请选择关注类型', trigger: 'change' } ],
        comment: [ { required: true, message: '请输入备注信息', trigger: 'blur' } ],
      },
      visible_report :false,
      form_report:{},
      student:null  //当前学生
   }
  },
  // 计算属性
  computed: {
    ...mapGetters(['channels','directionClazzTree'])
  },
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    // 取消关注
    toCancelAttentionHandler(row){
      this.$confirm('是否确认取消关注?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = '/student/cancelAttention'
        get(url,{id:row.id}).then(resp =>{
          this.$message({ type: 'success', message: resp.message });
          this.loadStudents();
        })
      })
    },
    // 提交关注
    submitAttentionHandler(){
      this.$refs['attentionForm'].validate((valid) => {
        if (valid) {
          let url = "/student/attention"
          post(url,this.form).then( resp => {
            this.$message({type:'success',message:resp.message})
            this.visible = false;
            this.loadStudents();
          })
        } else {
          return false
        }
      })
    },
     // 重点关注学生
    attentionStudentClass({row, rowIndex}){
      let flag = '';
      switch(row.isAttention){
        case '关系学生':
          flag = 'success-row'
          break;
        case '问题学生':
          flag = 'warning-row'
          break;
        default:
          flag = ''
      }
      return flag;
    },
     // 批量确认
    toBatchEditTuitionHandler(){
      if(this.students.length>0) {
        this.visible_tuition = true;
      } else {
        this.$message({ message: '请选择要确认的学生', type: 'error' })
      }
    },
    // 去修改学费信息
    toEditTuitionHandler(row){
      this.students = [row];
      this.form_tuition = row;
      this.visible_tuition = true;
    },
    //修改学费
    editTuitionHandler(){
       this.$refs['confirm_apply_form'].validate((valid) => {
        if (valid) {
          let ids = this.students.map(item => item.id)
          this.form_tuition.ids = ids;
          let url = "/student/batchUpdateTuition"
          post(url,this.form_tuition).then(response => {
            this.$message({ message: response.message, type: 'success' })
            this.loadStudents();
            this.visible_tuition = false;
          })
        } else {
          return false
        }
      })
    },
    //报道
    reportHandler(){
      this.$refs['report_form'].validate((valid) => {
        if (valid) {
          let url = "/student/report"
          post(url,this.form_report).then(resp => {
            this.$message({ message: resp.message, type: 'success' })
            this.visible_report = false;
            this.loadStudents();
          });
        } else {
          return false
        }
      })
    },
    // 去报道
    toReportHandler(row){
      this.form_report = _.clone(row);
      this.form_report.reportTime = new Date().getTime();
      this.visible_report = true;
    },
    
     // 去关注
    toAttentionHandler(row){
      this.form.id = row.id;
      this.visible = true;
    },
    // 查询宿舍类型信息
    loadDormitoryTypes(){
      // 添加loading,页面加载开始
      let url = "/dormitoryType/findAll"
      get(url).then(response => {
        this.dormitoryTypes = response.data;
      })
    },
    
    handleSelectionChange(val) {
      this.students = val;
    },
    // 下载数据
   downloadHandler(){
      // 去除空值
      let param = _.clone(this.param)
      for(let key in param){
        if(!param[key]){
          delete param[key]
        }
      }
     
      window.location.href = "http://218.4.196.178:8008/student/download?"+qs.stringify(param,{ arrayFormat: 'repeat' });
    },
    // 加载报名信息
    loadStudents(){
      this.loading = true;
      let url = '/student/pageQuery'
      // 去除空值
      for(let key in this.param){
        if(!this.param[key]){
          delete this.param[key]
        }
      }
      
      get(url+"?"+qs.stringify(this.param,{ arrayFormat: 'repeat' }))
      .then(response => {
        this.studentData = response.data
        this.loading = false;
      })
    },
   
    // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadStudents();
    },
     // 处理分页大小变化的函数
    handleSizeChange(pageSize){
      // 当前页为第一页
      this.page = 1;
      // 页面大小为用户选择的页面
      this.param.pageSize = pageSize;
      this.loadStudents();
    },
    // 处理visible变化的钩子函数，解决抽屉无法收缩的bug
    handlerVisibleChange(val) {
      this.visible = val
    }
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    // 加载报名信息
    this.loadStudents();
   
    // 加载宿舍类型
    this.loadDormitoryTypes();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style lang="scss" scoped>

</style>
